import json
import os

from api_qiyeweixin.apis.base import Base
from api_qiyeweixin.untils.log_until import logger
from api_qiyeweixin.untils.yaml_util import YamlUtil

proxies = {
    "http": "http://192.168.1.107:8080",
    "https": "http://192.168.1.107:8080",
}

class WeWork(Base):

    def __init__(self):
        env = os.getenv("env", "prod")
        self.category = os.getenv("category", "department")
        self.date = YamlUtil.get_yaml(f"{env}_config.yml")
        self.token = None
        self.get_token()

    def get_token(self):
        r_dict = {
            "method": "GET",
            "url": f"{self.date['access_url']}",
            "params": {
                "corpid": self.date["corpid"],
                "corpsecret": self.date["corpsecret"]
            }
        }

        req = self.send_req(r_dict)

        logger.debug(f'get_token请求接口信息：{req.text}')

        self.token = req.json()["access_token"]

    def send_api(self, action, date_dict):
        url = f'{self.date["url"][self.category]}/{action}?access_token={self.token}'
        date_dict.setdefault("url", url)

        r = self.send_req(date_dict)

        logger.info(f'{action}返回的信息：{json.dumps(r.json())}')

        return r


if __name__ == '__main__':
    k = WeWork()
    k.get_token()

    data = {
        "method": "GET"
    }
    k.send_api("list", data)
